#※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
# File Name: install_python.sh
# Author: GID5564
# Description: Python相应版本的安装
# Created Time: 22/11/23-21:09:19
#※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
#! /usr/bin/env bash

function install_python(){
    #保存当前目录
    local cur=$(pwd)
    [[ -z "$1" ]] && {
        error "Usage: install_python <Version>, Example:  Version=3.11.2 "
        return $INVALID_ARGUMENTS
    }
    local version=$1 #版本号,如:3.11.2
    
    local isver=$(echo ${version} | grep -o "[0-9]\{1,3\}\.[0-9]\{1,3\}.[0-9]\{1,3\}")
    [[ ${isver} ]] && {
        local py_version=${version%.*} #3.11
        #安装目录
        local install_path=/usr/local/python${py_version//./}
        msg "${BLUE}即将安装${RESET}${YELLOW}python${version}${RESET}"
        msg "${BLUE}安装路径为${RESET}${YELLOW}${install_path}${RESET}"
        
        #更新源
        sudo apt update && apt upgrade -y
            
        #安装相关包及依赖
        sudo apt-get install wget sudo build-essential libncurses-dev libreadline-dev libsqlite3-dev libssl-dev libexpat1-dev zlib1g-dev libffi-dev
        

        # 创建安装目录文件夹
        mkdir -p $install_path
        cd ${install_path}
            
        msg "正在下载Python${version}"
        # 使用官方网址下载--速度可能比较慢
        # wget https://www.python.org/ftp/python/$version/Python-$version.tgz
        # 使用国内华为镜像源下载python
        wget https://mirrors.huaweicloud.com/python/$version/Python-$version.tgz
            
        msg "${BLUE}正在解压…${RESET}"
        tar -xzf Python-$version.tgz
    
        msg "${BLUE}解压完成，移除压缩包${RESET}"
        rm -rf Python-$version.tgz
            
        msg "${BLUE}正在编译安装…${RESET}"
        tip "${YELLOW}安装时间有点长，请耐心等待一下…${RESET}"
        cd Python-$version
            
        #添加环境变量
        echo "export PATH=${install_path}/bin:$PATH" >>/etc/profile
        echo "export PATH=${install_path}/bin:$PATH" >>${FILE_ZSH}

        export PATH=${install_path}/bin:$PATH

        export LD_LIBRARY_PATH=/usr/lib/:$LD_LIBRARY_PATH
        
        #指定安装路径
        ./configure --prefix=$install_path  
        #编译安装
        make -j4 && make altinstall
            
        msg "${BLUE}配置软连接${RESET}"
        warn "${RED}如果系统有依赖Python其它版本的请谨慎修改!${RESET}"
        Msg "${BLUE}确定要修改吗? [${RESET}${YELLOW}y${RESET}${BLUE}|${RESET}${YELLOW}n${RESET}${BLUE}]${REAET} "
        read -n 1 yn
        SpecChar '\n'
        case ${yn} in
            y|Y)
                #移除已有软连接
                sudo rm -rf /usr/bin/python3
                sudo rm -rf /usr/bin/python
                #建立新的软连接
                sudo ln -s ${install_path}/bin/python${py_version} /usr/bin/python3
                sudo ln -s ${install_path}/bin/python${py_version} /usr/bin/python
                ;;
            *)
                tip "要使用此版本时,请按命令: ${install_path}/bin/python${py_version}"
                ;;
        esac
            
        cd ..
        rm -rf Python-$version
        tip "完成安装Python-$version"
            
        cd ${install_path}/bin
        msg "${BLUE}测试一下是否成功安装。${RESET}"
        python${py_version} -v
        #退出
        unset py_version
        unset install_path
    }
    cd ${cur}
    unset version
    unset isver
    unset cur
}


#编译安装最新版opencv
function install_opencv(){
    local cur=$(pwd)
    sudo apt update && apt upgrade -y
    sudo apt install -y make cmake gcc g++
    
    #要支持python2，请执行以下操作：
    #sudo apt-get安装python-dev python-numpy
    #要支持python3，请执行以下操作：
    sudo apt install -y python3-dev python3-numpy
    #接下来我们需要GTK支持GUI功能、摄像机支持(V4L)、媒体支持(ffmpeg、gstreamer)等。

    sudo apt install -y libavcodec-dev libavformat-dev libswscale-dev
    sudo apt install -y libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev
    sudo apt install -y libavresample-dev libdc1394-22-dev

    #要支持gtk2：
    sudo apt-get install libgtk2.0-dev
    #要支持gtk3：
    sudo apt-get install libgtk-3-dev
    #可选依赖项-图片
    sudo apt-get install libpng-dev
    sudo apt-get install libjpeg-dev
    sudo apt-get install libopenexr-dev
    sudo apt-get install libtiff-dev
    sudo apt-get install libwebp-dev
    
    
    git clone https://gitee.com/gid5564/opencv.git
    
    cd opencv
    #创建构建文件夹并进入
    mkdir build
    cd build
    #构建配置:用于配置OpenCV库构建(从构建文件夹执行)：
    cmake ../
    #编译安装
    make && make install
    cd ${pwd}
    unset cur
}






















